package fem2.analysis;

import math2.FunctionRnToR;
import math2.RampFunctionRToR;
import fem2.Constraint;
import fem2.Model;
import fem2.strategies.NonAdaptiveNewtonRaphsonStrategy;

/**
 * Perform multi-step static or quasi static analysis. The time start at 1.0 and
 * end by nt<br>
 * Note: this analysis will modify time factor at load and constraint to achieve
 * result. So use it with case with dynamics analysis
 * 
 * @author hbui
 * 
 */
public class NonlinearStaticAnalysis extends Analysis {

	public NonlinearStaticAnalysis(Model m, int nt) {
		super(m);
		setStrategy(new NonAdaptiveNewtonRaphsonStrategy(m, nt));
		if (nt > 1) {
			initializeTimeFactor(nt);
		}
	}

	/**
	 * initilialize time factor for load and constraint
	 * 
	 * @param nt
	 */
	public void initializeTimeFactor(int nt) {
		getStrategy().setNumberOfTimeStep(nt);
		/*
		 * create lambda
		 */
		FunctionRnToR lambda = new RampFunctionRToR(0, nt, 0.0, 1.0);
		/*
		 * set time factor for all the load
		 */
		for (int i = 0; i < getModel().countLoads(); i++) {
			getModel().getLoad(i).setTimeFactor(lambda);
		}
		/*
		 * set time factor for all the constraint
		 */
		for (int i = 0; i < getModel().getMesh().countNodes(); i++) {
			Constraint c = getModel().getMesh().getNode(i).getConstraint();
			if (c != null) {
				c.setTimeFactor(lambda);
			}
		}
	}

}
